package org.example.lease.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;

import org.apache.commons.codec.digest.DigestUtils;
import org.example.lease.model.entities.SystemUser;
import org.example.lease.model.enums.BaseStatus;
import org.example.lease.model.result.Result;
import org.example.lease.service.SystemUserService;
import org.example.lease.vo.system.user.SystemUserItemVo;
import org.example.lease.vo.system.user.SystemUserQueryVo;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;

/**
 * @author 宋孟林
 * @description
 * @date: 2025/5/24 20:59
 */
@Slf4j
@RestController
@RequestMapping("/admin/system/user")
@Tag(name = "后台用户信息管理")
public class SystemUserController {
    @Resource
    private SystemUserService systemUserService;

    /**
     * 参数列表中的 @RequestBody SystemUserQueryVo systemUserQueryVo 其实也可以不加 @RequestBody注解，不加
     * 注解的话前端可以选择传递参数和不传递参数，如果写了注解就一定要传递参数，所以这样会导致前后端联调失败
     * @param current c
     * @param size s
     * @param systemUserQueryVo vo
     * @return result
     */
    @Operation(summary = "根据条件分页查询后台用户列表")
    @GetMapping("/page")
    public Result<IPage<SystemUserItemVo>> selectPageForSysUserItemVoByQuery(@RequestParam Long current,
                                                                             @RequestParam Long size,
                                                                             SystemUserQueryVo systemUserQueryVo) {
        Page<SystemUserItemVo> page = new Page<>(current, size);
        IPage<SystemUserItemVo> systemUserItemVoIPage = systemUserService.selectPageForSysUserItemVoByQuery(page, systemUserQueryVo);
        return Result.ok(systemUserItemVoIPage);
    }

    /**
     * 隐藏了password字段和update_time等
     *
     * @param id id
     * @return result
     */
    @Operation(summary = "根据ID查询后台用户信息")
    @GetMapping("/getById")
    public Result<SystemUserItemVo> getById(@RequestParam Long id) {
        SystemUserItemVo systemUserItemVo = systemUserService.selectSystemUserItemVoById(id);
        return Result.ok(systemUserItemVo);
    }

    @Operation(summary = "保存或者更新用户信息")
    @PostMapping("/saveOrUpdate")
    public Result<?> saveOrUpdateSysUserInfo(@RequestBody SystemUser systemUser) {
        String password = systemUser.getPassword();
        if (password != null) {
            systemUser.setPassword(DigestUtils.md5Hex(password));
        }
        systemUserService.saveOrUpdate(systemUser);
        return Result.ok();
    }

    @Operation(summary = "判断后台用户名是否可用")
    @GetMapping("isUserNameAvailable")
    public Result<Boolean> isUsernameExists(@RequestParam String username) throws JsonProcessingException {
        Boolean flag = true;
        SystemUser systemUser = systemUserService.getOne(Wrappers.lambdaQuery(SystemUser.class).eq(SystemUser::getUsername, username));
        if (systemUser != null) {
            flag = false;
        }
        return Result.ok(flag);
    }
//    @Operation(summary = "判断后台用户名是否可用")
//    @GetMapping("isUserNameAvailable")
//    public Result<Boolean> isUsernameExists(@RequestParam String username) {
//        LambdaQueryWrapper<SystemUser> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(SystemUser::getUsername, username);
//        long count = service.count(queryWrapper);
//        return Result.ok(count == 0);
//    }

    @DeleteMapping("/deleteById")
    @Operation(summary = "根据ID删除后台用户信息")
    public Result<?> removeById(@RequestParam Long id) {
        systemUserService.removeById(id);
        return Result.ok();
    }

    @Operation(summary = "根据ID修改后台用户状态")
    @PostMapping("/updateStatusByUserId")
    public Result<?> updateStatusByUserId(@RequestParam Long id, @RequestParam BaseStatus status) {
        systemUserService.update(Wrappers.lambdaUpdate(SystemUser.class).eq(SystemUser::getId, id).set(SystemUser::getStatus, status));
        return Result.ok();
    }
}
